﻿namespace CSharp4
{
    /// <summary>
    /// calendar
    /// </summary>
    internal class program1
    {//设置月份宽度、月份高度、日期间距
        const int monthwidth = 40;
        const int daywidth = 4;
        const int monthheight = 9;
        static void Main(string[] args)
        {  //光标位置初始化为（0，0）
            int x = 0;
            int y = 0;
            //设置初始年月日
            DateTime theDay = new DateTime(2022, 1, 1);
            //打印2022年的每一天
            for (theDay = new DateTime(2022, 1, 1); theDay.Year != 2023; theDay = theDay.AddDays(1))
            {   //记住每日对应星期几
                int Week = (int)theDay.DayOfWeek;

                if (theDay.Day == 1)
                {  //计算月份所在位置并打印
                    x = (theDay.Month - 1) % 3 * monthwidth;
                    y = (theDay.Month - 1) / 3 * monthheight;
                    Console.SetCursorPosition(x, y);
                    Console.Write($"{theDay.Month}月".PadLeft(16, ' '));
                    //计算星期所在位置并打印
                    y++;
                    Console.SetCursorPosition(x, y);
                    Console.Write("  日  一  二  三  四  五  六  ");
                    //计算每月1号所在位置并打印
                    y++;
                    x = (theDay.Month - 1) % 3 * monthwidth + Week * daywidth;
                    Console.SetCursorPosition(x, y);
                    Console.Write("1".PadLeft(4, ' '));
                }
                //日期为星期六时，计算日期所在位置并打印，并使光标跳转到下一行相应位置
                if (Week == 6)
                {
                    x = (theDay.Month - 1) % 3 * monthwidth + 6 * daywidth;
                    Console.SetCursorPosition(x, y);
                    Console.Write(theDay.Day.ToString().PadLeft(4, ' '));
                    y++;
                    x = x - 6 * daywidth;
                }
                //日期不为周六时，打印该日期并使光标向右移动一个日宽
                else
                {
                    Console.SetCursorPosition(x, y);
                    Console.Write(theDay.Day.ToString().PadLeft(4, ' '));
                    x = x + 4;
                }
            }
            Console.ReadKey();
        }
    }
}